import request from '@/utils/request';

/**
 * 订单确认详情
 * 文档 https://innovation.yuque.com/finesdk/fine-product/fo7h9a
 * 物车参数使用 idList，立即购买参数使用productItemRequestList。二者不能同时为空，同时有值时，默认使用购物车逻辑。
 * @param data
 * @returns {Promise<*>}
 */
export async function orderConfirmListOrderInfo(data, noToast) {
  return request({
    url: `/product/v1/order/confirmInfo/listOrderInfo`,
    method: 'POST',
    data: data,
    noToast,
  });
}

/**
 * 创建订单
 * @param data
 * @returns {Promise<*>}
 */
export async function productCarOrderCreateOrder(data, noToast) {
  return request({
    url: `/product/v1/product/product-car-order/createOrder`,
    method: 'POST',
    data: data,
    noToast,
  });
}

/**
 * 使用支付方式支付
 * @param data
 * @returns {Promise<*>}
 */
export async function toPayByPayType(data) {
  const channel = process.env.TARO_ENV === 'h5' ? CHANNEL_H5 : CHANNEL_MINI;
  return request({
    url: `/product/product/pay/toPayByPayType`,
    method: 'POST',
    data: { ...data, channel },
  });
}

// 获取订单可用积分
export async function getOrderPointServer(data) {
  return request({
    url: `/wallet/api/usablePoint/getUseDeductionPointApi`,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
    data,
  });
}

// 买单
export async function payBillService(data) {
  const channel = process.env.TARO_ENV === 'h5' ? CHANNEL_H5 : CHANNEL_MINI;
  return request({
    url: `/product/product/pay/payBill`,
    method: 'POST',
    data: { ...data, channel },
  });
}

export async function getOrgDistributionWayServer(data) {
  return request({
    url: `/product/product/distributionWay/select`,
    method: 'GET',
    data,
  });
}

export async function getUserAddressServer(data) {
  const { merchantId, orgId } = data;
  let url = '/product/order/confirmInfo/getUserAddress';
  if (merchantId && orgId) {
    url = `/product/order/confirmInfo/getUserAddress?merchantId=${merchantId}&orgId=${orgId}`;
  }
  return request({
    url,
    method: 'GET',
  });
}
